<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Detail;
use App\Admin\Actions\Grid\Ding\User;
use App\Admin\Repositories\Ding;
use App\Models\Department;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Illuminate\Support\Str;

class DingController extends AdminController
{

    public bool $show_column_selector = false;
//    public string $create_permission_name = 'ding-create';
//    public string $edit_permission_name = 'ding-edit';
//    public string $delete_permission_name = 'ding-delete';

    //更新部门
    public string $pull_department_permission_name = 'ding-department';
    //更新员工
    public string $pull_user_permission_name = 'ding-user';

    //是否有更新部门权限
//    public function pullDepartment(): bool
//    {
//        return Admin::user()->isAdministrator() || Admin::user()->can($this->pull_department_permission_name);
//    }
//
//    //是否有更新员工权限
//    public function pullUser(): bool
//    {
//        return Admin::user()->isAdministrator() || Admin::user()->can($this->pull_user_permission_name);
//    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Ding(), function (Grid $grid) {
            $grid->column('id');
            $grid->column('name');
            $grid->column('corp_id')->display(function ($value) {
                return Str::substr($value, 0, 3) . '……' . Str::substr($value, -3);
            })->copyable();
            $grid->column('app_key')->display(function ($value) {
                return Str::substr($value, 0, 3) . '……' . Str::substr($value, -3);
            });
            $grid->column('app_secret')->display(function ($value) {
                return Str::substr($value, 0, 3) . '……' . Str::substr($value, -3);
            });
            $grid->column('event_url')->display(function () {
                return  route('ding.push', ['id' => $this->id]);

            })->copyable();
            $grid->column('robot_url')->display(function () {
                return route('ding.robot', ['id' => $this->id]);
            })->copyable();
            if (config('admin.sms_login')) {
                $grid->column('sms_tpl');
            }
            $grid->column('created_at');

            $_this = $this;
            $grid->actions(function (Grid\Displayers\Actions $actions) use ($_this) {
                $actions->append(new Detail());
                    $actions->append(new \App\Admin\Actions\Grid\Ding\Department());
                    $actions->append(new User());
            });
            $this->gridCommon($grid);
            $grid->enableDialogCreate();
            $grid->showQuickEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public function detail(int $id): Show
    {
        return Show::make($id, new Ding(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('corp_id');
            $show->field('app_key');
            $show->field('app_secret');
            $show->field('token');
            $show->field('aes_key');
            $show->field('robot_code');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Ding(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->text('corp_id')->required();
            $form->text('app_key')->required();
            $form->text('app_secret')->required();
            $form->text('token')->required()->help('请输入事件订阅-签名token');
            $form->text('aes_key')->required()->help('请输入事件订阅-加密aes_key');
            $form->text('robot_code')->required()->help('请输入机器人编码');
            if (config('admin.sms_login')) {
                $form->text('sms_tpl')->maxLength(100);
            }
        });
    }


    public function destroy($id)
    {
        if (Department::query()->where('ding_id', $id)->count()) {
            return $this->form()->response()->error('存在部门信息,不可以删除');
        }
        return $this->form()->destroy($id);
    }
}
